package upe.mestrado.base;

import upe.mestrado.operadores.selecao.RouletteFitness;

public class Individuo implements Comparable<Individuo>{
	private double[] gene;
	private double fitness;
	private RouletteFitness rouletteFitness;

	public Individuo(double[] gene){
		this.gene = gene;
		rouletteFitness = null;
	}

	/**
	 * @return the gene
	 */
	public double[] getGene() {
		return gene;
	}
	/**
	 * @param gene the gene to set
	 */
	public void setGene(double[] gene) {
		this.gene = gene;
	}
	/**
	 * @return the fitness
	 */
	public double getFitness() {
		return fitness;
	}
	/**
	 * @param fitness the fitness to set
	 */
	public void setFitness(double fitness) {
		this.fitness = fitness;
	}

	public RouletteFitness getRouletteFitness() {
		return rouletteFitness;
	}

	public void setRouletteFitness(RouletteFitness rouletteFitness) {
		this.rouletteFitness = rouletteFitness;
	}

	@Override
	public int compareTo(Individuo individuo) {
		if(rouletteFitness == null){
			if(fitness < individuo.getFitness()){
				return -1;
			}
			else if(fitness > individuo.getFitness()){
				return 1;
			}
			return 0;
		}else{
			if(rouletteFitness.getPercOfFitness() < individuo.getRouletteFitness().getPercOfFitness()){
				return -1;
			}
			else if(rouletteFitness.getPercOfFitness() > individuo.getRouletteFitness().getPercOfFitness()){
				return 1;
			}
			return 0;
		}
	}
	
	@Override
	public boolean equals(Object obj) {
		
		if(!(obj instanceof Individuo)){
			return false;
		}
		
		Individuo referencia = (Individuo)obj;
		for (int i = 0; i < gene.length; i++) {
			if(gene[i] != referencia.getGene()[i])
				return false;
		}
		
		return true;
	}
	
	@Override
	public String toString() {
		return fitness + "";
	}
}
